import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';

import '../../themes/appTheme.dart';

// ignore: must_be_immutable
class RemoveFromFolderDialog extends StatefulWidget {
  RemoveFromFolderDialog({
    Key? key,
    required this.title,
    required this.selected,
    required this.message,
  }) : super(key: key);

  String title;
  final List<String> selected;
  String message;

  _RemoveFromFolderDialog createState() => new _RemoveFromFolderDialog();
}

class _RemoveFromFolderDialog extends State<RemoveFromFolderDialog> {
  final BooksController _booksController = Get.find();
  final box = Hive.box('appconfig');
  bool isTeacher = false;

  @override
  void initState() {
    isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    super.initState();
  }

  void _removeFromFolder() async {
    String? _folderId = _booksController.getCurrentBookFolder().id;
    Log.debug(_folderId);
    //_booksController.getCurrentBookFolder();
    _booksController
        .removeBooksFromLibraryFolder(_folderId!, widget.selected)
        .then((data) {
      if (data.message != null && data.status == 1000) {
        _booksController.selectedFavoriteReadingBookItems.clear();
        showSnackMsg(isTeacher
            ? 'Book(s) removed from lesson plan successfully'
            : 'Book(s) removed from folder successfully');
        Navigator.of(context).pop();
      } else {
        showSnackMsg(isTeacher
            ? 'Error to remove book(s) from lesson plan'
            : 'Error to remove book(s) from folder');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(widget.title),
      titleTextStyle: TextStyle(
          letterSpacing: 1,
          color: Get.isDarkMode ? Get.theme.primaryColorDark : Colors.blueGrey,
          fontSize: 20),
      backgroundColor:
          Get.isDarkMode ? AppTheme.colors.darkTheme2 : Colors.white,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
      content: Container(
        height: 200,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Padding(
                  padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                  child: Text(
                    widget.message,
                    style: TextStyle(fontSize: 20),
                  ),
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                MaterialButton(
                    color:
                        Get.isDarkMode ? (Color(0xff444444)) : (Colors.white),
                    textColor: Get.isDarkMode
                        ? (Color(0xffcccccc))
                        : Theme.of(context).primaryColorDark,
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: Text('Cancel'.tr)),
                SizedBox(
                  width: 20,
                ),
                MaterialButton(
                    color: Get.isDarkMode
                        ? (Colors.white)
                        : Theme.of(context).primaryColorDark,
                    textColor: Get.isDarkMode
                        ? (Colors.black)
                        : Theme.of(context).primaryColorLight,
                    onPressed: () {
                      _removeFromFolder();
                    },
                    child: Text('Submit'.tr))
              ],
            ),
          ],
        ),
      ),
    );
  }
}
